#include <Adafruit_Fingerprint.h>
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(2, 3);
unsigned char item1[4] = {0xAA, 0x02, 0x00, 0xAC};//播放
unsigned char item2[4] = {0xAA, 0x05, 0x00, 0xAF};//上一首
unsigned char item3[4] = {0xAA, 0x06, 0x00, 0xB0};//下一首
unsigned char item4[4] = {0xAA, 0x03, 0x00, 0xAD};//停止
unsigned char item5[5] = {0xAA, 0x13, 0x01, 0x14, 0xD0};//音量18较小
unsigned char item6[5] = {0xAA, 0x13, 0x01, 0x19, 0xD7};//音量25中值
unsigned char item7[5] = {0xAA, 0x13, 0x01, 0x19, 0xDA};//音量28最大
unsigned char item8[6] = {0xAA, 0x07, 0x02, 0x00, 0x01, 0xB4};//曲目1
unsigned char item9[6] = {0xAA, 0x07, 0x02, 0x00, 0x02, 0xB5};//曲目2
unsigned char item10[6]= {0xAA, 0x07, 0x02, 0x00, 0x03, 0xB6};//曲目3

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial2);

void setup()  
{	
  pinMode(6,INPUT);
  Serial.begin(9600);
  Serial3.begin(9600);
  while (!Serial); 
  delay(100);
  Serial.println("\n\nAdafruit finger detect test");
  finger.begin(57600);
  delay(5);
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1) { delay(1); }
  }

  finger.getTemplateCount();
  Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  Serial.println("Waiting for valid finger...");
}

void loop()                     // run over and over again
{
  getFingerprintIDez();
  delay(50);            //don't ned to run this at full speed.
  
}

uint8_t getFingerprintID() {
  uint8_t p = finger.getImage();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println("No finger detected");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }

  // OK success!

  p = finger.image2Tz();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }
  
  // OK converted!
  p = finger.fingerFastSearch();
  if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_NOTFOUND) {
    Serial.println("Did not find a match");
    return p;
  } else {
    Serial.println("Unknown error");
    return p;
  }   
  
  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence); 
  
  return finger.fingerID;
}

// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.image2Tz();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.fingerFastSearch();
  if (p != FINGERPRINT_OK)  return -1;
  
  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence);
  char sect;sect=finger.fingerID;
  int flag;flag=finger.confidence;
  if(sect == 1) jj1();
  if(sect == 2) jj2();
  if(sect == 3) jj3();
  if(sect == 4) jj4();
  if(sect == 5) jj5();
  if(sect == 6) jj6();
  if(sect == 7) jj7();
  if(sect == 8) jj8();
  if(sect == 9) jj9();
  if(sect == 10)jj10();
  if(flag >= 100) digitalWrite(13,HIGH);
  if(flag <= 100)  digitalWrite(13,LOW);
  
  return finger.fingerID; 
}
void jj1()
{
	for(int i=0 ; i<4;i++)
	{   
		Serial3.write(item1[i]);
    }
}
void jj2()
{
	for(int i=0 ; i<4;i++)
	{   
		Serial3.write(item2[i]);
    }
}
void jj3()
{
	for(int i=0 ; i<4;i++)
	{   
		Serial3.write(item3[i]);
    }
}
void jj4()
{
	for(int i=0 ; i<4;i++)
	{   
		Serial3.write(item4[i]);
    }
}
void jj5()
{
	for(int i=0 ; i<5;i++)
	{   
		Serial3.write(item5[i]);
    }
}
void jj6()
{
	for(int i=0 ; i<5;i++)
	{   
		Serial3.write(item6[i]);
    }
}
void jj7()
{
	for(int i=0 ; i<5;i++)
	{   
		Serial3.write(item7[i]);
    }
}
void jj8()
{
	for(int i=0 ; i<6;i++)
	{   
		Serial3.write(item8[i]);
    }
}
void jj9()
{
	for(int i=0 ; i<6;i++)
	{   
		Serial3.write(item9[i]);
    }
}
void jj10()
{
	for(int i=0 ; i<6;i++)
	{   
		Serial3.write(item10[i]);
    }
}